/**------------------------------------------------------------
 *
 * Copyright © 2014 rubbervalley. All rights reserved.
 *
 * @Title: MenuDirective.java
 * @Package: com.rubbervalley.datacenter.util
 * @Description: 系统菜单
 * @author: angus
 * @date: 下午5:24:37
 * @version: V1.0
 * 
 * Update History:
 * 
 ------------------------------------------------------------*/
package com.rubbervalley.uyo.directive;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import org.springframework.beans.factory.annotation.Autowired;

import com.rubbervalley.framework.config.Constants;
import com.rubbervalley.framework.system.business.ResourceService;
import com.rubbervalley.framework.system.domain.Resource;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author angus
 *
 */
public class MenuDirective implements TemplateDirectiveModel {

	@Autowired
	private Cache  resouceCache;
	
	@Autowired
	private ResourceService  resourceService;
	/* (non-Javadoc)
	 * @see freemarker.template.TemplateDirectiveModel#execute(freemarker.core.Environment, java.util.Map, freemarker.template.TemplateModel[], freemarker.template.TemplateDirectiveBody)
	 */
	@SuppressWarnings({"unchecked", "rawtypes" })
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 
			throws TemplateException, IOException 
	{
		// 参数解析
		Object objmodule = params.get("module");
		String module = objmodule==null?null:objmodule.toString();
		 
		Integer resouceId = -1;
		try
		{
			resouceId = Integer.parseInt(module);
		}catch(Exception ex){}
		java.util.List<Resource> resourcesCatch = (java.util.List<Resource>)resouceCache.get(Constants.CACHE_RESOURCE).getObjectValue();
		
		java.util.List<Resource> resources = new java.util.ArrayList<Resource>();
		resources = copyBySerialize(resourcesCatch);
		//resources.addAll((java.util.List<Resource>)resouceCache.get(Constants.CACHE_RESOURCE).getObjectValue());
		
		// 过滤出有权限的菜单
		resources = resourceService.selectChildTreeRightsResources(resources);
		env.setVariable("resources", ObjectWrapper.DEFAULT_WRAPPER.wrap(resources));
 
		Resource currentResource = null;
		Resource topResource = null;
		if(!resouceId.equals(-1))
		{
			currentResource = resourceService.findResource(resouceId, resources);
			topResource = resourceService.topResource(currentResource);
		}
		env.setVariable("currentResource", ObjectWrapper.DEFAULT_WRAPPER.wrap(currentResource));
		env.setVariable("topResource", ObjectWrapper.DEFAULT_WRAPPER.wrap(topResource));
		
		if (body != null) {  
			 body.render(env.getOut());  
	    }
	}  
	
	
	 @SuppressWarnings("rawtypes")
	public List copyBySerialize(List src) {
		 try{
	        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
	        ObjectOutputStream out = new ObjectOutputStream(byteOut);
	        out.writeObject(src);
	    
	        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
	        ObjectInputStream in =new ObjectInputStream(byteIn);
	        List dest = (List)in.readObject();
	        return dest;
		 }catch(Exception ex){}
		 return null;
	    }

}
